package com.huangfu.learning.javabase.java.enumeration;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Author: huangfu.daohui
 * Date: 2018/7/9
 * Time: 19:08
 * Version: 1.0
 * Description:常用  作为常量
 **/
public class EnumDemo {
    public static void main(String[] args) {
        // 普通用法
        Day day = Day.MONDAY;
        System.out.println(day);
        System.out.println("-------------------------------------");

        //创建枚举数组 两种
        Day[] dss = Day.values();
        System.out.println("ds" + dss);
        Day[] days = new Day[]{Day.MONDAY, Day.TUESDAY, Day.WEDNESDAY, Day.THURSDAY, Day.FRIDAY, Day.SATURDAY, Day.SUNDAY};
        for (Day d : days) {
            System.out.println(d);//遍历打印Days
        }
        System.out.println("-------------------------------------");

        //通过compareTo方法比较,实际上其内部是通过ordinal()值比较的
        System.out.println("days[0].compareTo(days[1]):" + days[0].compareTo(days[1])); // -1
        System.out.println("days[0].compareTo(days[1]):" + days[0].compareTo(days[2])); // -2
        System.out.println("-------------------------------------");

        //获取该枚举对象的Class对象引用,当然也可以通过getClass方法
        Class<?> clazz = days[0].getDeclaringClass();
        System.out.println("clazz:" + clazz);//clazz:com.huangfu.learning.javabase.java.enumeration.Day
        System.out.println("-------------------------------------");

        //name()
        System.out.println("days[0].name():" + days[0].name());
        System.out.println("days[1].name():" + days[1].name());
        System.out.println("days[2].name():" + days[2].name());
        System.out.println("days[3].name():" + days[3].name());
        System.out.println("-------------------------------------");

        //toString
        System.out.println("days[0].toString():" + days[0].toString());
        System.out.println("days[1].toString():" + days[1].toString());
        System.out.println("days[2].toString():" + days[2].toString());
        System.out.println("days[3].toString():" + days[3].toString());
        System.out.println("-------------------------------------");

        Day d1 = Enum.valueOf(Day.class, days[0].name());
        Day d2 = Day.valueOf(Day.class, days[0].name());
        System.out.println("d1:" + d1 + ";  d2:" + d2);
        System.out.println("-------------------------------------");

        //正常使用
        Day[] ds = Day.values();
        //向上转型Enum
        Enum e = Day.MONDAY;
        //无法调用,没有此方法
        //e.values();
        //获取class对象引用
        Class<?> clasz = e.getDeclaringClass();
        if (clasz.isEnum()) {
            Day[] dsz = (Day[]) clasz.getEnumConstants();
            System.out.println("dsz:" + Arrays.toString(dsz));
        }
    }
}

enum Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
